package com.itheima.marking_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.marking_system.mapper.ElementMapper;
import com.itheima.marking_system.pojo.common.R;
import com.itheima.marking_system.pojo.entity.ElementEntity;
import com.itheima.marking_system.pojo.entity.PictureEntity;
import com.itheima.marking_system.service.ElementService;
import com.itheima.marking_system.service.PictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ElementServiceImpl extends ServiceImpl<ElementMapper, ElementEntity> implements ElementService {
    
    @Autowired
    private PictureService pictureService;

    
    @Override
    public List<ElementEntity> getElementList(Long zipId) {
        List<PictureEntity> pictureEntities = pictureService.getPictureList(zipId);

        List<ElementEntity> elementEntities = new ArrayList<>();
        for (PictureEntity picture : pictureEntities) {
            LambdaQueryWrapper<ElementEntity> elementQueryWrapper = new LambdaQueryWrapper<>();
            elementQueryWrapper.eq(ElementEntity::getPictureId, picture.getId());
            ElementEntity elementResult = this.getOne(elementQueryWrapper);
            if (elementResult == null) {
                throw new RuntimeException("元素不存在");
            }
            elementEntities.add(elementResult);
        }
        return elementEntities;
    }
}
